package com.coolpad.job.device.config;


import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;

import java.util.HashMap;

/**
 * 功能描述:<br/>
 *
 * @Author:shengtao_yang
 * @Date: 2020/12/21 13:26
 * @since: 1.0.0
 */
//@Configuration
public class KafkaConfig {

    @Value("${spring.kafka.bootstrap-servers}")
    private String bootstrap_servers_config;
    @Value("${spring.kafka.producer.retries}")
    private String retries_config;
    @Value("${spring.kafka.producer.batch-size}")
    private String batch_size_config;
    @Value("${spring.kafka.producer.buffer-memory}")
    private String buffer_memory_config;

    @Value("${spring.kafka.location.update.topic}")
    public String locationTopic;


    @Bean
    public KafkaTemplate<String,String> kafkaTemplate(){
        HashMap<String, Object> configs = new HashMap<>();
        configs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,bootstrap_servers_config);
        configs.put(ProducerConfig.RETRIES_CONFIG,retries_config);
        configs.put(ProducerConfig.BATCH_SIZE_CONFIG,batch_size_config);
        configs.put(ProducerConfig.BUFFER_MEMORY_CONFIG,buffer_memory_config);
        configs.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        configs.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class);
        DefaultKafkaProducerFactory producerFactory = new DefaultKafkaProducerFactory(configs);
        KafkaTemplate kafkaTemplate = new KafkaTemplate<String,String>(producerFactory);
        kafkaTemplate.setDefaultTopic(locationTopic);
        return kafkaTemplate;
    }

}
